using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CameraMovementComponent : Microsoft.Xna.Framework.GameComponent
    {
        bool moveCameraX = true;
        bool moveCameraY = true;
        bool switchDirection = false;

        Vector2 oldPlayerLocation;

        public CameraMovementComponent(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public void Update(GameTime gameTime, PlayerComponent player, MapComponent map, Camera2DComponent camera, int windowWidth, int windowHeight)
        {
            //Only move camera to the right
            if (player.GetXVel() < 0)
            {
                moveCameraX = false;

                //Store old location at the moment player changes direction
                if (switchDirection)
                {
                    oldPlayerLocation.X = player.GetLocation().X; //Store old location
                    switchDirection = false;
                }
            }
            else if (player.GetLocation().X > oldPlayerLocation.X)
            {
                moveCameraX = true;
                switchDirection = true;
            }

            //Do not move camera below map
            //Don't display last tile because of bounds
            if (player.GetLocation().Y + player.GetSize().Y / 2 + windowHeight / 2 < map.getMapDimensions().Y - 64)
                moveCameraY = true;
            else
                moveCameraY = false;


            //Move camera on x axis
            if (moveCameraX)
                camera.setPosition(new Vector2(player.GetLocation().X + player.GetSize().X / 2 - windowWidth / 2, camera.getPosition().Y));
            //Move camera on y axis
            if (moveCameraY)
                camera.setPosition(new Vector2(camera.getPosition().X, player.GetLocation().Y + player.GetSize().Y / 2 - windowHeight / 2));

           
            base.Update(gameTime);
        }
    }
}
